package se.jagvetintedu;

public class Problem17 {

	private static class NumToString {
		private static final String [] belowTen     = {"", "one", "two", "three", "four", "five", "six", "seven", "eigth", "nine"};
		private static final String [] tens         = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
		private static final String [] belowHundred = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
		
		public static String toString(Integer number) {
			if (number < 10) {
				return belowTen[number];
			}
			else if (number < 20) {
				return tens[number-10];
			}
			else if (number < 100) {
				StringBuilder retVal = new StringBuilder();
				retVal.append(belowHundred[number/10-2]);
				retVal.append(belowTen[number % 10]);
				return retVal.toString();
			}
			else if (number < 1000) {
				StringBuilder retVal = new StringBuilder();
				retVal.append(belowTen[number/100]);
				retVal.append(" hundred");
				if (number%100 != 0) {
					retVal.append(" and ");
				}
				retVal.append(toString(number%100));
				
				return retVal.toString();
			}
			else if (number == 1000) {
				return new String("one thousand");
			}
			return new String("");
		}
	}
	
	public Integer solve(Integer max) {
		
		StringBuilder numberString = new StringBuilder();
		
		for (Integer t = 1; t<=max; t++) {
			numberString.append(NumToString.toString(t));
		}
		
		return numberString.toString().replaceAll(" ", "").replaceAll("-", "").length();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("Project Euler, problem 17");
		
		Problem17 solution = new Problem17();

		System.out.println("number of way through a 20x20 grid are: " + solution.solve(1000));

	}

}
